﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acms.Bll;

public partial class Posts_Topics : BasePostPage
{
    private Role.Roles[] EditRoles = new Role.Roles[] { Role.Roles.Developer, Role.Roles.ContentEditor, Role.Roles.ContentPublisher };

    public override Role.Roles[] AllowedRoles
    {
        get { return new Role.Roles[] { Role.Roles.Developer, Role.Roles.ContentViewer, Role.Roles.ContentEditor, Role.Roles.ContentPublisher }; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            lnkEdit.Visible = User.HasRole(EditRoles);
            lnkSave.Visible = false;
            lnkCancel.Visible = false;
        }
    }

    protected void dsTopicGroups_SelectData(object sender, Acms.DataControls.SelectEventArgs<TopicGroup> e)
    {
        e.SetData(TopicGroup.GetAllBySiteId(AcmsContext.SiteId));
    }

    protected void itemCheckboxList_DataBinding(object sender, EventArgs e)
    {
        CheckBoxList itemCheckboxList = sender as CheckBoxList;
        if (itemCheckboxList != null)
        {
            //find the id
            Acms.WebControls.AcmsDataListItem item = itemCheckboxList.NamingContainer as Acms.WebControls.AcmsDataListItem;
            if (item != null)
            {
                int id = (int)dlTopicGroups.DataKeys[item.ItemIndex]["Id"];
                itemCheckboxList.DataSource = Topic.GetAllByTopicGroupId(id);
            }
        }
    }

    protected void itemCheckboxList_DataBound(object sender, EventArgs e)
    {
        CheckBoxList itemCheckboxList = sender as CheckBoxList;
        if (itemCheckboxList != null)
        {
            foreach (ListItem item in itemCheckboxList.Items)
            {
                item.Enabled = false;
                item.Selected = IsChecked(item.Value);
            }
        }
    }

    protected bool IsChecked(string id)
    {
        return GetChecked(id) != null;
    }

    protected PostTopic GetChecked(string id)
    {
        foreach (PostTopic topic in CurrentPostTopics)
        {
            if (topic.TopicId.ToString() == id)
            {
                return topic;
            }
        }
        return null;
    }

    private List<PostTopic> currentPostTopics;
    protected List<PostTopic> CurrentPostTopics
    {
        get 
        {
            if (currentPostTopics == null)
            {
                currentPostTopics = PostTopic.GetAllByPostId(PostId);
            }
            return currentPostTopics;
        }
    }

    protected void lnkEdit_Click(object sender, EventArgs e)
    {
        foreach (Acms.WebControls.AcmsDataListItem item in dlTopicGroups.Items)
        {
            CheckBoxList itemCheckboxList = item.FindControl("itemCheckboxList") as CheckBoxList;
            if (itemCheckboxList != null)
            {
                foreach (ListItem checkBox in itemCheckboxList.Items)
                {
                    checkBox.Enabled = true;
                }
            }
        }
        lnkEdit.Visible = false;
        lnkSave.Visible = true;
        lnkCancel.Visible = true;
    }

    protected void lnkSave_Click(object sender, EventArgs e)
    {
        Guid transactionKey = Guid.Empty;
        foreach (Acms.WebControls.AcmsDataListItem item in dlTopicGroups.Items)
        {
            CheckBoxList itemCheckboxList = item.FindControl("itemCheckboxList") as CheckBoxList;
            if (itemCheckboxList != null)
            {
                foreach (ListItem checkBox in itemCheckboxList.Items)
                {
                    PostTopic was = GetChecked(checkBox.Value);
                    if (was != null && !checkBox.Selected)
                    {
                        if (transactionKey == Guid.Empty)
                        {
                            was.BeginTransaction();
                            transactionKey = was.TransactionKey;
                        }
                        else
                        {
                            was.UseTransaction(transactionKey);
                        }
                        was.Delete();
                    }
                    else if(was == null && checkBox.Selected)
                    {
                        PostTopic tmp = PostTopic.CreateInstance(PostId, int.Parse(checkBox.Value));
                        if (transactionKey == Guid.Empty)
                        {
                            tmp.BeginTransaction();
                            transactionKey = tmp.TransactionKey;
                        }
                        else
                        {
                            tmp.UseTransaction(transactionKey);
                        }
                        tmp.Update();
                    }
                    checkBox.Enabled = false;
                }
            }
        }
        if (transactionKey != Guid.Empty)
        {
            TopicGroup tmp = TopicGroup.CreateInstance();
            tmp.UseTransaction(transactionKey);
            tmp.CommitTransaction();
        }
        lnkEdit.Visible = true;
        lnkSave.Visible = false;
        lnkCancel.Visible = false;
    }

    protected void lnkCancel_Click(object sender, EventArgs e)
    {
        foreach (Acms.WebControls.AcmsDataListItem item in dlTopicGroups.Items)
        {
            CheckBoxList itemCheckboxList = item.FindControl("itemCheckboxList") as CheckBoxList;
            if (itemCheckboxList != null)
            {
                foreach (ListItem checkBox in itemCheckboxList.Items)
                {
                    checkBox.Enabled = false;
                }
            }
        }
        lnkEdit.Visible = true;
        lnkSave.Visible = false;
        lnkCancel.Visible = false;
    }
    protected void dlTopicGroups_ItemCreated(object sender, Acms.WebControls.AcmsDataListItemEventArgs e)
    {
        if (e.Item.IsDataItem)
        {
            ajaxManager.AjaxSettings.AddAjaxSetting(actions, e.Item);
        }
    }

    public override void GetBreadCrumbs(BaseMasterPage.BreadCrumbCollection breadcrumbs)
    {
        base.GetBreadCrumbs(breadcrumbs);
        breadcrumbs.Add("Topics");
    }

}
